package com.nowcoder.community.config;

import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

@Configuration
public class KaptchaCofig {

    @Bean
    public Producer kaptchaProducer(){
        //2.2 创建properties对象，添加配置
        Properties properties =new Properties();
        properties.setProperty("kaptcha.image.width","100");
        properties.setProperty("kaptcha.image.height","100");
        properties.setProperty("kaptcha.textproducer.font.size","32");
        properties.setProperty("kaptcha.textproducer.font.color","0,0,0");
        //字符选取范围
        properties.setProperty("kaptcha.textproducer.char.String","0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
        //字符选取长度
        properties.setProperty("kaptcha.textproducer.char.length","4");
        //添加噪声干扰:文件搜索nonoise，包名+后缀
        properties.setProperty("kaptcha.noise.impl","com.google.code.kaptcha.impl.NoNoise");

        //1.实例化核心类
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        //2.将核心类的参数封装到Config对象
        //2.1创建Config对象:Config对象需要传入Property对象
        Config config = new Config(properties);
        //3.将Config对象传入核心类
        defaultKaptcha.setConfig(config);

        return defaultKaptcha;
    }
}
